package purchaseStore

import (
	"context"
	"errors"
	"github.com/google/uuid"
	"github.com/kuangshp/go-utils/k"
	"insighttracking/apps/admin/auth"
	"insighttracking/common/utils"
	"insighttracking/query/model"
	"insighttracking/query/service"
	"net/http"

	"insighttracking/apps/admin/internal/svc"
	"insighttracking/apps/admin/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetPurchaseStoreDetailApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewGetPurchaseStoreDetailApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *GetPurchaseStoreDetailApiLogic {
	hostname, _ := os.Hostname()
	return &GetPurchaseStoreDetailApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *GetPurchaseStoreDetailApiLogic) GetPurchaseStoreDetailApi(req *types.IdReq) (resp *types.GetPurchaseStoreDetailResp, err error) {
	accountInfo, err := auth.GetAccountInfo(l.svcCtx, l.Request)
	if err != nil {
		return nil, err
	}
	storeEntity, err := service.PurchaseStoreService.GetPurchaseStoreByIdApi(l.ctx, accountInfo.EnterpriseId, req.Id)
	if err != nil {
		return nil, errors.New("传递的主键错误")
	}
	// 查询详情
	detailEntities, err := service.PurchaseStoreDetailService.GetPurchaseStoreByParentIdApi(l.ctx, req.Id)
	if err != nil {
		return nil, errors.New("查询失败")
	}
	var dateList = k.Map(detailEntities, func(item *model.PurchaseStoreDetailEntity, index int) types.PurchaseStoreDetailModel {

		return types.PurchaseStoreDetailModel{
			ID:                      item.ID,                                 // 主键id
			PurchaseStoreID:         item.PurchaseStoreID,                    // 采购入库单purchase_store表主键id
			SpuID:                   item.SpuID,                              // 商品,关联到spu表主键id
			SnapshotSpuName:         item.SnapshotSpuName,                    // 快照商品名称
			SnapshotProductImageURL: item.SnapshotProductImageURL,            // 快照产品图片
			SnapshotSpuType:         item.SnapshotSpuType,                    // 快照商品类型,0表示基础商品,1表示加工商品
			SnapshotUnit:            item.SnapshotUnit,                       // 快照单位
			SnapshotCategoryID:      item.SnapshotCategoryID,                 // 快照分类id
			SnapshotCategory:        item.SnapshotCategory,                   // 快照分类
			SnapshotPrice:           item.SnapshotPrice.StringFixed(2),       // 快照商品单价
			LossRate:                item.LossRate.StringFixed(2),            // 快照损耗率
			PurchaserPrice:          item.PurchaserPrice.StringFixed(2),      // 采购单价
			PurchaserCount:          item.PurchaserCount.StringFixed(2),      // 采购数量
			PurchaserAmount:         item.PurchaserAmount.StringFixed(2),     // 采购总额
			StoreCount:              item.StoreCount.StringFixed(2),          // 入库数量
			StoreAmount:             item.StoreAmount.StringFixed(2),         // 入库金额
			StoreStatus:             item.StoreStatus,                        // 入库状态,-1表示取消,0表示已入库
			Remark:                  item.Remark,                             // 备注
			CreatedAt:               utils.TimestampToString(item.CreatedAt), // 创建时间
			UpdatedAt:               utils.TimestampToString(item.UpdatedAt), // 更新时间
		}
	})
	var operateName = ""
	if storeEntity.OperateID > 0 {
		personEntity, err := service.EnterprisePersonService.GetByIdApi(l.ctx, storeEntity.OperateID)
		if err == nil {
			operateName = personEntity.Name
		}
	}

	return &types.GetPurchaseStoreDetailResp{
		BaseData: types.PurchaseStoreModel{
			ID:                    storeEntity.ID,                                   // 主键id
			PurchaseOrdersID:      storeEntity.PurchaseOrdersID,                     // 采购单purchase_orders表主键id,如果是pc创建的时候为0
			PurchaseOrdersOrderNo: storeEntity.PurchaseOrdersOrderNo,                // 采购单流水号
			OrderNo:               storeEntity.OrderNo,                              // 操作流水号
			SupplierID:            storeEntity.SupplierID,                           // 供应商,关联到supplier表主键id
			SupplierName:          storeEntity.SupplierName,                         // 供应商名称
			PurchaserID:           storeEntity.PurchaserID,                          // 采购员,关联到
			PurchaserName:         storeEntity.PurchaserName,                        // 采购员名称
			OperateDate:           utils.TimestampToString(storeEntity.OperateDate), // 收货日期
			OperateName:           operateName,                                      // 制单员
			OperateID:             storeEntity.OperateID,                            // 制单员id,关联到员工表
			WarehouseID:           storeEntity.WarehouseID,                          // 入库仓,关联到warehouse表主键id
			WarehouseName:         storeEntity.WarehouseName,                        // 仓库名称
			TotalCount:            storeEntity.TotalCount.StringFixed(2),            // 采购总数量
			Source:                storeEntity.Source,                               // 来源,0表示采购单生成,1表示手动创建单
			Types:                 storeEntity.Types,                                // 类型,0表示供应商创建,1表示现场采购
			Status:                storeEntity.Status,                               // 状态,10表示部分收货,20表示全部收货
			Remark:                storeEntity.Remark,                               // 备注
			CreatedAt:             utils.TimestampToString(storeEntity.CreatedAt),   // 创建时间
			UpdatedAt:             utils.TimestampToString(storeEntity.UpdatedAt),   // 更新时间
			CreatedID:             storeEntity.CreatedID,                            // 创建人
			CreatedName:           storeEntity.CreatedName,                          // 创建人
		},                  // 基础数据
		DataList: dateList, //商品数据
	}, nil
}
